<template>
  <div class="login">
    <!-- 背景遮罩 -->
    <div class="login-bg-overlay"></div>
    
    <div class="login-container">
      <!-- 左侧内容区域 -->
      <div class="login-left">
        <div class="login-logo">
          <div class="logo-icon">
            <svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
              <circle cx="50" cy="30" r="20" fill="#409EFF" opacity="0.8"/>
              <path d="M30 70 L70 70 L50 50 Z" fill="#67C23A" opacity="0.8"/>
              <path d="M40 45 L60 45" stroke="#FFF" stroke-width="2"/>
              <path d="M50 35 L50 55" stroke="#FFF" stroke-width="2"/>
            </svg>
          </div>
          <h1 class="system-title">{{systemTitle}}</h1>
          <p class="system-subtitle">{{systemSubtitle}}</p>
        </div>
        
        <!-- 系统功能简介 -->
        <div class="system-features">
          <h3>系统功能</h3>
          <ul>
            <li>• 环保数据实时监控与分析</li>
            <li>• 多维度数据可视化展示</li>
            <li>• 智能数据分析与预警</li>
            <li>• 历史数据查询与对比</li>
            <li>• 数据报表自动生成</li>
          </ul>
        </div>
      </div>
      
      <!-- 右侧登录表单 -->
      <div class="login-right">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <h3 class="form-title">用户登录</h3>
          
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="用户名"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
          
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right;" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    
    <!-- 底部 -->
    <div class="el-login-footer">
      <span>{{copyright}}</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      systemTitle: "环保数据分析系统",
      systemSubtitle: "基于大数据的智能环保决策平台",
      copyright: "Copyright © 2023-2025 环保数据分析系统 版权所有",
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

/* 背景遮罩和装饰 */
.login-bg-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url("../assets/images/login-background.jpg");
  background-size: cover;
  background-position: center;
  opacity: 0.15;
  z-index: 0;
}

/* 装饰元素 */
.login-bg-overlay::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(72,187,120,0.1) 0%, rgba(72,187,120,0) 70%);
  animation: float 20s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translate(0, 0) rotate(0deg); }
  25% { transform: translate(5%, 5%) rotate(5deg); }
  50% { transform: translate(0, 10%) rotate(0deg); }
  75% { transform: translate(-5%, 5%) rotate(-5deg); }
}

/* 登录容器 */
.login-container {
  display: flex;
  width: 900px;
  max-width: 90%;
  min-height: 500px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  z-index: 1;
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

/* 左侧内容区域 */
.login-left {
  flex: 1;
  padding: 40px;
  background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.login-logo {
  text-align: center;
  margin-bottom: 40px;
}

.logo-icon {
  width: 100px;
  height: 100px;
  margin: 0 auto 20px;
  svg {
    width: 100%;
    height: 100%;
  }
}

.system-title {
  font-size: 28px;
  font-weight: bold;
  margin: 0 0 10px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.system-subtitle {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
}

/* 系统功能介绍 */
.system-features {
  margin-top: 30px;
}

.system-features h3 {
  font-size: 18px;
  margin-bottom: 15px;
  font-weight: 500;
}

.system-features ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.system-features li {
  padding: 8px 0;
  font-size: 14px;
  opacity: 0.9;
  transition: all 0.3s ease;
}

.system-features li:hover {
  transform: translateX(5px);
  opacity: 1;
}

/* 右侧登录表单 */
.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.login-form {
  width: 100%;
  max-width: 320px;
}

.form-title {
  text-align: center;
  color: #2d3748;
  font-size: 24px;
  font-weight: 500;
  margin-bottom: 30px;
}

.login-form .el-form-item {
  margin-bottom: 20px;
}

.login-form .el-input {
  height: 42px;
  border-radius: 6px;
  .el-input__inner {
    height: 42px;
    border-radius: 6px;
    border-color: #e2e8f0;
    transition: all 0.3s ease;
    &:focus {
      border-color: #48bb78;
      box-shadow: 0 0 0 3px rgba(72, 187, 120, 0.1);
    }
  }
}

.input-icon {
  color: #a0aec0;
}

.login-code {
  width: 33%;
  height: 42px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
    border-radius: 6px;
    transition: transform 0.3s ease;
    &:hover {
      transform: scale(1.05);
    }
  }
}

.login-code-img {
  height: 42px;
  width: 100%;
  object-fit: cover;
}

.login-form .el-button--primary {
  background-color: #48bb78;
  border-color: #48bb78;
  height: 42px;
  border-radius: 6px;
  font-size: 16px;
  transition: all 0.3s ease;
  &:hover {
    background-color: #38a169;
    border-color: #38a169;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(72, 187, 120, 0.3);
  }
  &:active {
    transform: translateY(0);
  }
}

.link-type {
  color: #48bb78;
  transition: color 0.3s ease;
  &:hover {
    color: #38a169;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  z-index: 1;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
    min-height: auto;
  }
  
  .login-left {
    order: 2;
    padding: 30px 20px;
  }
  
  .login-right {
    order: 1;
    padding: 30px 20px;
  }
  
  .system-title {
    font-size: 24px;
  }
  
  .logo-icon {
    width: 80px;
    height: 80px;
  }
  
  .form-title {
    font-size: 20px;
  }
}

@media (max-width: 480px) {
  .login-container {
    margin: 20px;
    width: calc(100% - 40px);
  }
  
  .login-left,
  .login-right {
    padding: 20px;
  }
  
  .system-features {
    display: none;
  }
}
</style>
